// RabbitMQ 消息队列封装

use anyhow::Result;
use lapin::{
    options::{BasicPublishOptions, QueueDeclareOptions},
    types::FieldTable,
    BasicProperties, Channel, Connection, ConnectionProperties,
};

/// 创建 RabbitMQ 连接
pub async fn create_connection(url: &str) -> Result<Connection> {
    let conn = Connection::connect(url, ConnectionProperties::default()).await?;
    tracing::info!("RabbitMQ connection established");
    Ok(conn)
}

/// 发布消息到队列
pub async fn publish_message(
    channel: &Channel,
    queue_name: &str,
    message: &[u8],
) -> Result<()> {
    // 声明队列（如果不存在）
    channel
        .queue_declare(
            queue_name,
            QueueDeclareOptions::default(),
            FieldTable::default(),
        )
        .await?;

    // 发布消息
    channel
        .basic_publish(
            "",
            queue_name,
            BasicPublishOptions::default(),
            message,
            BasicProperties::default(),
        )
        .await?
        .await?;

    Ok(())
}
